﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Nevron.Chart;
using System.Collections;
using Nevron.GraphicsCore;
using Nevron;
using System.Configuration;
using System.IO;

namespace IGuess
{
    public partial class MAPForm : Form
    {

        private ClassifierForm form;
        private MAPClassifier classifier;
        private string fileName;

        public MAPForm(ClassifierForm form, MAPClassifier classifier, string fileName)
        {
            InitializeComponent();
            this.form = form;
            this.classifier = classifier;
            this.fileName = fileName;
        }

        private void MAPForm_Load(object sender, EventArgs e)
        {
            this.loadResources();
            this.MAPFormResultTextBox.Text = this.classifier.Classify();
            this.generateDEStats();
            this.generateENStats();
            this.generateFRStats();
            this.writeStats();
            Bitmap flag;

            if (this.classifier.Result.Equals("EN"))
            {
                flag = new Bitmap(@".\Resources\englishFlag.png");
                this.pictureBox1.Image = flag;
            }
            else if (this.classifier.Result.Equals("FR"))
            {
                flag = new Bitmap(@".\Resources\frenchFlag.gif");
                this.pictureBox1.Image = flag;
            }
            else if (this.classifier.Result.Equals("DE"))
            {
                flag = new Bitmap(@".\Resources\germanFlag.JPG");
                this.pictureBox1.Image = flag;
            }

            this.label8.Text = String.Format("{0:0.00}", this.classifier.ScoreEN.ToString());
            this.label7.Text = String.Format("{0:0.00}", this.classifier.ScoreFR.ToString());
            this.label6.Text = String.Format("{0:0.00}", this.classifier.ScoreDE.ToString());
            this.label5.Text = String.Format("{0:0.00}", this.classifier.NumberOfWords.ToString());
        }

        public void writeStats()
        {
            string[,] statsFile = new string[3, 4];

            if (File.Exists(ConfigurationManager.AppSettings["StatisticsFile"]))
            {

                //Read the file and store its content
                using (System.IO.StreamReader sr = new System.IO.StreamReader(ConfigurationManager.AppSettings["StatisticsFile"]))
                {
                    string line;
                    int i = 0;
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] token = line.Split();
                        statsFile[i, 0] = token[0];
                        statsFile[i, 1] = token[1];
                        statsFile[i, 2] = token[2];
                        statsFile[i, 3] = token[3];
                        i++;
                    }

                }

                if (this.fileName.ToLower().Contains("en") && classifier.Result.ToLower().Equals("en"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[0, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[0, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[0, 3]);

                    statsFile[0, 1] = (totalENTexts + 1).ToString();
                    statsFile[0, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("de") && classifier.Result.ToLower().Equals("de"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[1, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[1, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[1, 3]);

                    statsFile[1, 1] = (totalENTexts + 1).ToString();
                    statsFile[1, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("fr") && classifier.Result.ToLower().Equals("fr"))
                {

                    int totalENTexts = Convert.ToInt32(statsFile[2, 1]);
                    int CorrectENTexts = Convert.ToInt32(statsFile[2, 2]);
                    int totalAnalizedTexts = Convert.ToInt32(statsFile[2, 3]);

                    statsFile[2, 1] = (totalENTexts + 1).ToString();
                    statsFile[2, 2] = (CorrectENTexts + 1).ToString();
                    statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                    statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else
                {

                    if (this.fileName.ToLower().Contains("en"))
                    {

                        int totalENTexts = Convert.ToInt32(statsFile[0, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[0, 3]);

                        statsFile[0, 1] = (totalENTexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("de"))
                    {

                        int totalDETexts = Convert.ToInt32(statsFile[0, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[1, 3]);

                        statsFile[1, 1] = (totalDETexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("fr"))
                    {

                        int totalENTexts = Convert.ToInt32(statsFile[2, 1]);
                        int totalAnalizedTexts = Convert.ToInt32(statsFile[2, 3]);

                        statsFile[2, 1] = (totalENTexts + 1).ToString();
                        statsFile[0, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[1, 3] = (totalAnalizedTexts + 1).ToString();
                        statsFile[2, 3] = (totalAnalizedTexts + 1).ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                            statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                            statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }

                }


            }
            else
            {

                if (this.fileName.ToLower().Contains("en") && classifier.Result.ToLower().Equals("en"))
                {

                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 1.ToString();
                    statsFile[0, 2] = 1.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 0.ToString();
                    statsFile[1, 2] = 0.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 0.ToString();
                    statsFile[2, 2] = 0.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("de") && classifier.Result.ToLower().Equals("de"))
                {


                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 0.ToString();
                    statsFile[0, 2] = 0.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 1.ToString();
                    statsFile[1, 2] = 1.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 0.ToString();
                    statsFile[2, 2] = 0.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else if (this.fileName.ToLower().Contains("fr") && classifier.Result.ToLower().Equals("fr"))
                {


                    statsFile[0, 0] = "EN";
                    statsFile[0, 1] = 0.ToString();
                    statsFile[0, 2] = 0.ToString();
                    statsFile[0, 3] = 1.ToString();
                    statsFile[1, 0] = "DE";
                    statsFile[1, 1] = 0.ToString();
                    statsFile[1, 2] = 0.ToString();
                    statsFile[1, 3] = 1.ToString();
                    statsFile[2, 0] = "FR";
                    statsFile[2, 1] = 1.ToString();
                    statsFile[2, 2] = 1.ToString();
                    statsFile[2, 3] = 1.ToString();

                    string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                    {

                        file.WriteLine(fileContent);

                    }

                }
                else
                {

                    if (this.fileName.ToLower().Contains("en"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 1.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 0.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 0.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("de"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 0.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 1.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 0.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }
                    else if (this.fileName.ToLower().Contains("fr"))
                    {


                        statsFile[0, 0] = "EN";
                        statsFile[0, 1] = 0.ToString();
                        statsFile[0, 2] = 0.ToString();
                        statsFile[0, 3] = 1.ToString();
                        statsFile[1, 0] = "DE";
                        statsFile[1, 1] = 0.ToString();
                        statsFile[1, 2] = 0.ToString();
                        statsFile[1, 3] = 1.ToString();
                        statsFile[2, 0] = "FR";
                        statsFile[2, 1] = 1.ToString();
                        statsFile[2, 2] = 0.ToString();
                        statsFile[2, 3] = 1.ToString();

                        string fileContent = statsFile[0, 0] + " " + statsFile[0, 1] + " " + statsFile[0, 2] + " " + statsFile[0, 3] + "\n" +
                        statsFile[1, 0] + " " + statsFile[1, 1] + " " + statsFile[1, 2] + " " + statsFile[1, 3] + "\n" +
                        statsFile[2, 0] + " " + statsFile[2, 1] + " " + statsFile[2, 2] + " " + statsFile[2, 3];

                        using (System.IO.StreamWriter file = new System.IO.StreamWriter(ConfigurationManager.AppSettings["StatisticsFile"]))
                        {

                            file.WriteLine(fileContent);

                        }

                    }

                }

            }


        }

        public void loadResources()
        {

            this.MAPFormAccetpButton.Text = RM.GetString("MAPFormAcceptButton");
            this.MAPFormDEStatsGroupBox.Text = RM.GetString("MAPFormDEStatsGroupBox"); ;
            this.MAPFormENStatsGroupBox.Text = RM.GetString("MAPFormENStatsGroupBox");
            this.MAPFormFRStatsGroupBox.Text = RM.GetString("MAPFormFRStatsGroupBox");
            this.MAPFormResultGroupBox.Text = RM.GetString("MAPFormResultGroupBox");

        }

        public void generateENStats()
        {

            // configure the chart
            NChart chart = MAPFormENChart.Charts[0];
            chart.Width = 550;
            chart.Height = 300;

            // add interlace stripe
            NLinearScaleConfigurator linearScale = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator;
            NScaleStripStyle stripStyle = new NScaleStripStyle(new NColorFillStyle(Color.Beige), null, true, 0, 0, 1, 1);
            stripStyle.Interlaced = true;
            stripStyle.SetShowAtWall(ChartWallType.Back, true);
            stripStyle.SetShowAtWall(ChartWallType.Left, true);
            linearScale.StripStyles.Add(stripStyle);

            // bar series
            NBarSeries series1 = (NBarSeries)chart.Series.Add(SeriesType.Bar);
            series1.Name = "Bar Series";
            series1.BorderStyle.Width = new NLength(0, NGraphicsUnit.Pixel);
            series1.ShadowStyle.Type = ShadowType.GaussianBlur;
            series1.ShadowStyle.Offset = new NPointL(2, 2);
            series1.ShadowStyle.Color = Color.FromArgb(80, 0, 0, 0);
            series1.ShadowStyle.FadeLength = new NLength(5);
            series1.HasBottomEdge = false;
            series1.DataLabelStyle.Visible = false;

            GenerateDataEN(series1);

            series1.Legend.Mode = SeriesLegendMode.DataPoints;

            NStyleSheet styleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.NevronMultiColor);
            styleSheet.Apply(MAPFormENChart.Document);

        }

        public void generateDEStats()
        {

            // configure the chart
            NChart chart = MAPFormDEChart.Charts[0];
            chart.Width = 550;
            chart.Height = 300;

            // add interlace stripe
            NLinearScaleConfigurator linearScale = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator;
            NScaleStripStyle stripStyle = new NScaleStripStyle(new NColorFillStyle(Color.Beige), null, true, 0, 0, 1, 1);
            stripStyle.Interlaced = true;
            stripStyle.SetShowAtWall(ChartWallType.Back, true);
            stripStyle.SetShowAtWall(ChartWallType.Left, true);
            linearScale.StripStyles.Add(stripStyle);

            // bar series
            NBarSeries series1 = (NBarSeries)chart.Series.Add(SeriesType.Bar);
            series1.Name = "Bar Series";
            series1.BorderStyle.Width = new NLength(0, NGraphicsUnit.Pixel);
            series1.ShadowStyle.Type = ShadowType.GaussianBlur;
            series1.ShadowStyle.Offset = new NPointL(2, 2);
            series1.ShadowStyle.Color = Color.FromArgb(80, 0, 0, 0);
            series1.ShadowStyle.FadeLength = new NLength(5);
            series1.HasBottomEdge = false;
            series1.DataLabelStyle.Visible = false;

            // fill with random data
            GenerateDataDE(series1);

            series1.Legend.Mode = SeriesLegendMode.DataPoints;

            NStyleSheet styleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.NevronMultiColor);
            styleSheet.Apply(MAPFormDEChart.Document);

        }

        public void generateFRStats()
        {

            // configure the chart
            NChart chart = MAPFormFRChart.Charts[0];
            chart.Width = 550;
            chart.Height = 300;

            // add interlace stripe
            NLinearScaleConfigurator linearScale = chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator;
            NScaleStripStyle stripStyle = new NScaleStripStyle(new NColorFillStyle(Color.Beige), null, true, 0, 0, 1, 1);
            stripStyle.Interlaced = true;
            stripStyle.SetShowAtWall(ChartWallType.Back, true);
            stripStyle.SetShowAtWall(ChartWallType.Left, true);
            linearScale.StripStyles.Add(stripStyle);

            // bar series
            NBarSeries series1 = (NBarSeries)chart.Series.Add(SeriesType.Bar);
            series1.Name = "Bar Series";
            series1.BorderStyle.Width = new NLength(0, NGraphicsUnit.Pixel);
            series1.ShadowStyle.Type = ShadowType.GaussianBlur;
            series1.ShadowStyle.Offset = new NPointL(2, 2);
            series1.ShadowStyle.Color = Color.FromArgb(80, 0, 0, 0);
            series1.ShadowStyle.FadeLength = new NLength(5);
            series1.HasBottomEdge = false;
            series1.DataLabelStyle.Visible = false;

            // fill with random data
            GenerateDataFR(series1);

            series1.Legend.Mode = SeriesLegendMode.DataPoints;

            NStyleSheet styleSheet = NStyleSheet.CreatePredefinedStyleSheet(PredefinedStyleSheet.NevronMultiColor);
            styleSheet.Apply(MAPFormFRChart.Document);

        }

        private void GenerateDataEN(NSeries series)
        {

            series.Values.Clear();
            FillWithData(series, this.classifier.HtEN);

        }

        private void FillWithData(NSeries series, Hashtable hs)
        {
            foreach (DictionaryEntry d in hs)
            {
                series.AddDataPoint(new NDataPoint(Convert.ToDouble(d.Value), d.Key.ToString()));
            }
        }

        private void GenerateDataDE(NSeries series)
        {

            series.Values.Clear();
            FillWithData(series, this.classifier.HtDE);

        }

        private void GenerateDataFR(NSeries series)
        {

            series.Values.Clear();
            FillWithData(series, this.classifier.HtFR);

        }

        private void MAPForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.form.Enabled = true;
        }

        private void MAPFormAccetpButton_Click(object sender, EventArgs e)
        {
            this.form.Enabled = true;
            this.Close();
        }

    }
}
